package com.itheima.iteratorlearn;

import java.util.ArrayList;
import java.util.Collection;

public class ContainsTest {
    public static void main(String[] args){
        Collection c = new ArrayList();
        String a1= new String("abc");
        c.add(a1);
        String a2= new String("def");
        c.add(a2);
        String a3= new String("abc");
        //String中重写了equals方法，所以contains调用时比较的时对象的内容，返回true
        System.out.println(c.contains(a3));


        User u1 = new User("张三");
        Collection c2 = new ArrayList();
        c2.add(u1);
        User u2 = new User("张三");

        //contains中的逻辑是调用equals方法判断两个对象的地址是否相同，所以返回false
        //System.out.println(c2.contains(u2));


        //改写User中的equals方法，使得contains在调用equals方法时，比较对象的内容，返回true
        System.out.println(c2.contains(u2));

        c2.remove(u1);
    }




}


class User{
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public boolean equals(Object obj){
        if(!(obj instanceof User))return false;
        if(obj==this)return true;
        return this.name.equals(((User) obj).name);
    }




}